﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

    public partial class ShopDetail : System.Web.UI.Page
    {
        string strUploadImagesPath = System.Configuration.ConfigurationManager.AppSettings["UploadImagesPath"];
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    BindData();
                    Binding.BindShopType(ddlShopType, true, "0", "โปรดระบุ");
                    Binding.BindPackage(ddlPackage, true, "0", "โปรดระบุ");
                }
            }
            catch (Exception ex)
            {
                lblMessage.Text = "<br>" + ex.Message + "<br><br>";
            }         
        }

        private void BindData()
        {
            if (Request.QueryString["result"] != null && Request.QueryString["result"].ToString() == "1")
            {
                    lblMessage.Text = "<br>แก้ไขข้อมูลเรียบร้อยแล้ว<br>";
            }
            DataTable dt = new DataTable();
            DBConn oDBConn = new DBConn();
            oDBConn.OpenConnection();

            SqlCommand oCommand = new SqlCommand("sp_Deal_Shop_SELECTBYSHOPID", oDBConn.DBConnection);
            oCommand.CommandType = CommandType.StoredProcedure;
            oCommand.Parameters.Add("@Shop_ID", SqlDbType.BigInt).Value = Request.QueryString["ShopID"];

            SqlDataAdapter oDataAdapter = new SqlDataAdapter(oCommand);
            oDataAdapter.Fill(dt);
            oDataAdapter = null;
            oDBConn.Disconnect();

            if (dt.Rows.Count > 0)
            {
                lblShopID.Text = dt.Rows[0]["Shop_ID"].ToString();
                txtShopName.Text = dt.Rows[0]["Shop_Name"].ToString();
                txtAddress.Text = dt.Rows[0]["Shop_Address"].ToString();
                txtLatitude.Text = dt.Rows[0]["Shop_Lat"].ToString();
                txtLongitude.Text = dt.Rows[0]["Shop_Long"].ToString();
                txtShopDetail.Text = dt.Rows[0]["Shop_Detail"].ToString();
                ddlPackage.SelectedValue = dt.Rows[0]["Shop_Package_ID"].ToString();
                ddlShopType.SelectedValue = dt.Rows[0]["Shop_ShopType_ID"].ToString();
                hdnShopLogo.Value = dt.Rows[0]["Shop_Logo"].ToString();
                hdnShopImage.Value = dt.Rows[0]["Shop_Image"].ToString();
             }
              
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = new DataTable();
                DBConn oDBConn = new DBConn();
                oDBConn.OpenConnection();
                //Upload Image
                bool blnValidUploadLogo = false;
                bool blnValidUploadImage = false;
                string ShopID = Request.QueryString["ShopID"];
                string imgPath = strUploadImagesPath + ShopID ;
                string pathHtmlLogo = "";
                Int32 intLimitImagesSize = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["LimitImagesSizeUploadKb"]);
                if (flUploadShopLogo.HasFile)
                {
                    string strExtension = Global.GetFileExtension(flUploadShopLogo.FileName).ToLower();
                    if (strExtension.Contains(".gif") || strExtension.Contains(".jpg") || strExtension.Contains(".jpeg") || strExtension.Contains(".png"))
                    {
                        //File Size Upload
                        if (flUploadShopLogo.FileBytes.Length > 1024 * intLimitImagesSize)
                        {
                            lblMessage.Text = "<br>ขนาดไฟล์โลโก้ร้านค้าเกิน " + intLimitImagesSize + " KB<br>";
                            blnValidUploadLogo = false;
                        }
                        else
                        {
                            flUploadShopLogo.SaveAs(Server.MapPath(imgPath + "/" + flUploadShopLogo.FileName));
                            pathHtmlLogo = "http://www.poximo.com/images/shopimages/" + ShopID + "/" + flUploadShopLogo.FileName;
                            blnValidUploadLogo = true;
                        }
                    }
                    else
                    {
                        lblMessage.Text = "<br>ประเภทไฟล์โลโก้ร้านค้าไม่ถูกต้อง<br>";
                        blnValidUploadLogo = false;
                    }
                }
                else
                {
                    pathHtmlLogo = hdnShopLogo.Value;
                    blnValidUploadLogo = true;
                }

                string pathHtmlImage = "";
                if (flUploadShopImage.HasFile)
                {
                    string strExtension = Global.GetFileExtension(flUploadShopImage.FileName).ToLower();
                    if (strExtension.Contains(".gif") || strExtension.Contains(".jpg") || strExtension.Contains(".jpeg") || strExtension.Contains(".png"))
                    {
                        //File Size Upload
                        if (flUploadShopImage.FileBytes.Length > 1024 * intLimitImagesSize)
                        {
                            lblMessage.Text = "<br>ขนาดไฟล์รูปภาพร้านค้าเกิน " + intLimitImagesSize + " KB<br>";
                            blnValidUploadImage = false;
                        }
                        else
                        {
                            flUploadShopImage.SaveAs(Server.MapPath(imgPath + "/" + flUploadShopImage.FileName));
                            pathHtmlImage = "http://www.poximo.com/images/shopimages/" + ShopID + "/" + flUploadShopImage.FileName;
                            blnValidUploadImage = true;
                        }
                    }
                    else
                    {
                        lblMessage.Text = "<br>ประเภทไฟล์รูปภาพร้านค้าไม่ถูกต้อง<br>";
                        blnValidUploadImage = false;
                    }
                }
                else
                {
                    pathHtmlImage = hdnShopImage.Value;
                    blnValidUploadImage = true;
                }
                if (blnValidUploadLogo && blnValidUploadImage)
                {
                    SqlCommand oCommand = new SqlCommand("sp_Deal_Shop_UPDATE", oDBConn.DBConnection);
                    oCommand.CommandType = CommandType.StoredProcedure;
                    oCommand.Parameters.Add("@Shop_ID", SqlDbType.BigInt).Value = ShopID;
                    oCommand.Parameters.Add("@Shop_Name", SqlDbType.VarChar).Value = txtShopName.Text.Trim();
                    oCommand.Parameters.Add("@Shop_Address", SqlDbType.VarChar).Value = txtAddress.Text.Trim();
                    oCommand.Parameters.Add("@Shop_Lat", SqlDbType.Float).Value = txtLatitude.Text.Trim();
                    oCommand.Parameters.Add("@Shop_Long", SqlDbType.Float).Value = txtLongitude.Text.Trim();
                    oCommand.Parameters.Add("@Shop_Detail", SqlDbType.VarChar).Value = txtShopDetail.Text.Trim();
                    oCommand.Parameters.Add("@Shop_Logo", SqlDbType.VarChar).Value = pathHtmlLogo;
                    oCommand.Parameters.Add("@Shop_Package_ID", SqlDbType.Int).Value = ddlPackage.SelectedValue;
                    oCommand.Parameters.Add("@Shop_ShopType_ID", SqlDbType.Int).Value = ddlShopType.SelectedValue;
                    oCommand.Parameters.Add("@Shop_Image", SqlDbType.VarChar).Value = pathHtmlImage;

                    oCommand.ExecuteNonQuery();
                    oDBConn.Disconnect();

                    Response.Redirect("ShopDetail.aspx?ShopID=" + ShopID + "&result=1");
                }
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("Shop.aspx");
        }
    }
